/**
 * Created with IntelliJ IDEA.
 * User: duoba
 * Date: 16/3/1
 * Time: 下午5:31
 * To change this template use File | Settings | File Templates.
 */

package com.cake21.uitest.Page;

import com.cake21.uitest.Constant.Constants;
import com.cake21.uitest.System.Core.Page;
import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.FindBy;

import java.util.List;

public class CakeRecommendPage extends Page {
    /*页面元素*/
    @FindBy(css = ".goods-item")
    private List<WebElement> goodsList;
    @FindBy(css = ".goods-pic")
    private List<WebElement> goodsImgList;
    @FindBy(css = ".goods-price")
    private List<WebElement> goodsPrice;
    @FindBy(className = "alert_ok")
    private WebElement popupBtn;

    /*载入页面*/
    public void load() {
        driver.get(Constants.CakeRecommendURL);
    }

    /*获得推荐蛋糕列表数量*/
    public int getGoodsCount() {
        return goodsList.size();
    }

    /*检查所有蛋糕图片*/
    public boolean allImgLoaded() {
        int imageCount = goodsImgList.size();
        /*先检查蛋糕图片数量*/
        if (imageCount > 1) {
            for (WebElement el : goodsImgList) {
                WebElement imageDiv = el.findElement(By.className("action-goods-img"));
                String imageAddress = imageDiv.getAttribute("src");
                /*检查图片URL是否包含JPG,因为如果没load出来,则为Gif格式,或者src没load到,则不会有JPG字符*/
                if (imageAddress.contains("jpg?")) {
                } else {
                    return false;
                }
            }
            return true;
        } else {
            return false;
        }
    }

    /*检查配送时间PopUp*/
    public String getShipTimePopUp() {
        /*检查数量*/
        int priceCount = goodsPrice.size();
        /*取第一个蛋糕作为Test*/
        if (priceCount > 0) {
            WebElement el = goodsPrice.get(0);
            WebElement xx = el.findElement(By.className("timeTooltip"));
            xx.click();
            String cakeName = el.findElement(By.className("title")).getText();
            String shipTimeEarly = el.findElement(By.className("early-time")).getText();
            String shipTimeLate = el.findElement(By.className("last-time")).getText();
            if (cakeName != "" && shipTimeEarly != "" && shipTimeLate != "") {
                return cakeName + " # " + shipTimeEarly + " # " + shipTimeLate;
            } else {
                return "";
            }
        } else {
            return "";
        }
    }

    /*弹出框确认*/
    public void confirmPopUpBtn() {
        popupBtn.click();
    }
}
